package com.atguigu.dga.assessor.storage;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.constant.MetaConst;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;

@Component("LIFECYCLE")
public class LifecycleAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception {
        //获取生命周期建议天数
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject jsonObject = JSONObject.parseObject(metricParamsJson);
        String days = jsonObject.getString("days");
        Integer integer = Integer.valueOf(days);

        String lifecycleType = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getLifecycleType();
        //判断周期类型
        if (lifecycleType==null || "UNSET".equals(lifecycleType)){
            //分数置零
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            //提示
            governanceAssessDetail.setAssessProblem("未设置周期类型");


        }else if(MetaConst.LIFECYCLE_TYPE_DAY.equals(lifecycleType)){
            //获取分区信息
            String partitionColNameJson = assessParam.getTableMetaInfo().getPartitionColNameJson();
            //获取生命周期
            Long lifecycleDays = assessParam.getTableMetaInfo().getTableMetaInfoExtra().getLifecycleDays();
            if (partitionColNameJson==null || partitionColNameJson.trim().isEmpty()){
                //分数置零
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                //提示
                governanceAssessDetail.setAssessProblem("未设置分区信息");
            }else if (lifecycleType ==null || lifecycleDays<=0) {
                //分数置零
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                //提示
                governanceAssessDetail.setAssessProblem("未设置生命周期");

            }else if(lifecycleDays<integer){
                double socre=lifecycleDays*10.0/integer;
                governanceAssessDetail.setAssessScore(BigDecimal.valueOf(socre));
                governanceAssessDetail.setAssessProblem("设定的生命周期超出规范");
            }

        }
        //获取通用路径
        String governanceUrl = assessParam.getGovernanceMetric().getGovernanceUrl();
        //路径替换
        governanceUrl=governanceUrl.replace("{tableId}",assessParam.getTableMetaInfo().getId() + "");
        governanceAssessDetail.setGovernanceUrl(governanceUrl);
    }
}
